<?php

require_once 'login.php';
require_once 'conexion.php';
require_once 'objects/person.php';
require_once 'adm/actionsEnterDisease.php';
require_once 'adm/actionsEnterFood.php';
require_once 'adm/actionsEnterRace.php';
require_once 'user/actionsEnterLand.php';
require_once 'user/actionsEnterBatch.php';
require_once 'user/actionsEnterCattle.php';

new servlet();

class servlet {
    
    private $conx;
    private $promPD;
    private $promPC;
    private $promC;

    public function __construct() {
        $action = $_GET['action'];
        if ($action == 'singin') {
            $this->loguear();
        } else if($action == 'enterDisease'){
            $this->regDisease();
        } else if($action == 'regFood'){
            $this->regFood();
        } else if($action == 'enterRace'){
            $this->regRace();
        } else if($action  == 'enterLand'){
            $this->regLand();
        } else if ($action == 'enterbatch'){
            $this->regBatch();  
        } else if ($action == 'register'){
            $this->regUser();
        } else if ($action == 'enterCattlePD'){
            $this->regCattlePD();
        }else {
            header("");
        }
    }

    public function loguear() {
        $log = new login();
        $log->login($_POST['email'], $_POST['passwd']);
    }
    
    public function regDisease(){
        $disease = new actionsEnterDiseace();
        $diseasecod = $disease->thisdisease();
        $diseasename = $_POST['namedisease'];
        $symptom = $_POST['symptoms'];
        $tratamient = $_POST['tratamient'];
        $sql = "INSERT INTO DISEASE VALUES (".$diseasecod.",
                                            '".$diseasename."',
                                            '".$symptom."',
                                            '".$tratamient."');";
        $con = sprintf($sql);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $sql);
        $this->conx->closeConexionDB();
        $this->conx = null;
        header("location: adm/menuadm.php");
    }
    
    public function regFood(){
        $food = new actionsEnterFood();
        $foodcod = $food->thisfood();
        $namefood = $_POST['namefood'];
        $drymatter = $_POST['dryMatterFood'];
        $crudeprotein = $_POST['crudeProtein'];
        $ndt = $_POST['ndt'];
        $em = $_POST['em'];
        $enm = $_POST['enm'];
        $eng = $_POST['eng'];
        $Ca = $_POST['calcium'];
        $P = $_POST['phosphorus'];
        $sql = "INSERT INTO FOOD VALUES (".$foodcod.",
                                        '".$namefood."',
                                         ".$drymatter.",
                                         ".$crudeprotein.",
                                         ".$ndt.",
                                         ".$em.",
                                         ".$enm.",
                                         ".$eng.",
                                         ".$Ca.",
                                         ".$P.");";
        $con = sprintf($sql);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $sql);
        $this->conx->closeConexionDB();
        $this->conx = null;
        header("location: adm/menuadm.php");
    }
    
    public function regRace(){
        $race = new actionsEnterRace();
        $racecod = $race->thisrace();
        $namerace = $_POST['namerace'];
        $weightbirth = $_POST['birthWeight'];
        $weightweaning = $_POST['weaningWeight'];
        $weightsacr = $_POST['sacrWeight'];
        $ageweanig = $_POST['weaningAge'];
        $agesacr = $_POST['sacrAge'];
        $drymatter = $_POST['dryMatter'];
        $tenderness = $_POST['tenderness'];
        $canalperformance = $_POST['efficiency'];
        $weightcanal = ($weightsacr*$canalperformance)/100;
        $beforeweaning = ($weightweaning-$weightbirth)/$ageweanig;
        $afterweaning = (($weightsacr-$weightweaning)/($agesacr-$ageweanig));
        $imagerace = $_POST['imgrace'];
        $sql = "INSERT INTO RACE VALUES (".$racecod.",
                                        '".$namerace."',
                                        ".$weightbirth.",
                                        ".$weightweaning.",
                                        ".$weightsacr.",
                                        ".$ageweanig.",
                                        ".$agesacr.",
                                        ".$drymatter.",
                                        ".$tenderness.",
                                        ".$canalperformance.",
                                        ".$weightcanal.",
                                        ".$beforeweaning.",
                                        ".$afterweaning.",
                                        '".$imagerace."');";
        $con = sprintf($sql);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $sql);
        $this->conx->closeConexionDB();
        $this->conx = null;
        header("location: adm/menuadm.php");      
    }
    
    public function regLand(){
        $land = new actionsEnterLand();
        $landcod = $land->thisland();
        $landname = $_POST['lname'];
        $departament = $_POST['depart'];
        $township = $_POST['tship'];
        $sidewalk = $_POST['swalk'];
        $userid = $_POST['userid'];
        $sql = "INSERT INTO LAND VALUES (".$landcod.",
                                        '".$landname."',
                                        '".$departament."',
                                        '".$township."',
                                        '".$sidewalk."',
                                         ".$userid.");";
        $con = sprintf($sql);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $sql);
        $this->conx->closeConexionDB();
        $this->conx = null;
        header("location: user/Registers.php");
    }
  
    public function regBatch(){
        $batch = new actionsEnterBatch();
        $d = date('d-m-Y');
        $batchcod = $batch->thisbatch();        
        $animalnum = $_POST['animalbatch'];
        $batchname = $_POST['bname'];
        $foodbatch = $_POST['fbatch'];
        $racebatch = $_POST['racebatch'];
        $landcod = $_POST['landcod'];
        $diseasecod = 'null';
        $dateregisterb =  $d;
        $sql = "INSERT INTO BATCH VALUES (".$batchcod.",
                                          ".$animalnum.",
                                         '".$batchname."',
                                          ".$foodbatch.",
                                          ".$racebatch.",
                                          ".$landcod.",
                                          ".$diseasecod.",
                                          '".$dateregisterb."');";                                          
        $con = sprintf($sql);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $sql);
        $this->conx->closeConexionDB();
        $this->conx = null;
        header("location: user/Registers.php");    
    }
    
    public function regUser(){
        $nameuser = $_POST['name'];
        $lastname = $_POST['last_name'];
        $doctype = $_POST['doctype'];
        $docnum = $_POST['numdoc'];
        $mail = $_POST['email'];
        $passwd = $_POST['passwd2'];
        $address = $_POST['address'];
        $phone = $_POST['phone'];
        $rolperson = 01;
        $sql = "INSERT INTO PERSON VALUES ('".$nameuser."',
                                          '".$lastname."',
                                          '".$doctype."',
                                           ".$docnum.",
                                          '".$mail."',
                                          '".$passwd."',
                                          '".$address."',
                                           ".$phone.",
                                           ".$rolperson.");";
        $con = sprintf($sql);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $sql);
        $this->conx->closeConexionDB();
        $this->conx = null;
        header("location: ../contact.php");      
    }
    
    public function regCattlePD(){
        $cattlePD = new actionsEnterCattle();
        $d = date('d-m-Y');
        $cattlecod = $cattlePD->thiscattle();        
        $birthdayca = $_POST['date1'];
        $weightbirth = $_POST['birthWeight1'];
        $weigthweaning = 'null';
        $weightact = $_POST['weigthAct1'];
        $ageact = $_POST['ageAct1'];
        $stage = 'Predestete';
        $batchcod = $_POST['batch1'];
        $dateregister = $d;
        $sql1 = "INSERT INTO CATTLE VALUES (".$cattlecod.",
                                           '".$birthdayca."',
                                            ".$weightbirth.",
                                            ".$weigthweaning.",
                                            ".$weightact.",
                                            ".$ageact.",
                                           '".$stage."',
                                            ".$batchcod.",
                                           '".$dateregister."');";                                         
        $con = sprintf($sql1);
        $this->conx = new conexion();
        $result = pg_exec($this->conx->getConexionDB(), $sql1);
        $this->conx->closeConexionDB();
        $this->conx = null;
        header("location: user/Registers.php");    
    }
    
}

?>
